package com.quectel.core.module.securitypatrol.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.securitypatrol.dao.SecurityPatrolSubjectDao;
import com.quectel.core.module.securitypatrol.dto.SecurityPatrolSubjectDto;
import com.quectel.core.module.securitypatrol.entity.SecurityPatrolSubjectEntity;
import com.quectel.core.module.securitypatrol.service.SecurityPatrolSubjectService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/05/07 14:09
 */
@DubboService
public class SecurityPatrolSubjectServiceImpl implements SecurityPatrolSubjectService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SecurityPatrolSubjectServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SECURITY_PATROL_SUBJECT_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SECURITY_PATROL_SUBJECT_CACHE_BY_ID:%d", id);

    @Autowired
    private SecurityPatrolSubjectDao securityPatrolSubjectDao;


    @Override
    public SecurityPatrolSubjectDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SECURITY_PATROL_SUBJECT_CACHE_BY_ID_FUNC.apply(id),

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SecurityPatrolSubjectDto selectById(Long id) {
        SecurityPatrolSubjectEntity entity = securityPatrolSubjectDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, SecurityPatrolSubjectDto.class));
    }

    @Override
    public List<SecurityPatrolSubjectDto> queryList(Map<String, Object> params) {
        List<SecurityPatrolSubjectEntity> list = securityPatrolSubjectDao.queryList(params);
        List<SecurityPatrolSubjectDto> result = CopyUtils.copyList(list, SecurityPatrolSubjectDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private SecurityPatrolSubjectDto paddingField(SecurityPatrolSubjectDto dto) {
        if (dto != null) {
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return securityPatrolSubjectDao.queryTotal(params);
    }

    @Override
    public Long save(SecurityPatrolSubjectDto dto) {
        SecurityPatrolSubjectEntity entity = CopyUtils.copyObj(dto, SecurityPatrolSubjectEntity.class);
        entity.setId(Snowflake.nextId());
        securityPatrolSubjectDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SecurityPatrolSubjectDto dto) {
        SecurityPatrolSubjectEntity entity = CopyUtils.copyObj(dto, SecurityPatrolSubjectEntity.class);
        securityPatrolSubjectDao.updateById(entity);
        CacheKit.invalidRedisCache(SECURITY_PATROL_SUBJECT_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void updateAllColumnById(SecurityPatrolSubjectDto dto) {
        SecurityPatrolSubjectEntity entity = CopyUtils.copyObj(dto, SecurityPatrolSubjectEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, securityPatrolSubjectDao);

        CacheKit.invalidRedisCache(SECURITY_PATROL_SUBJECT_CACHE_BY_ID_FUNC.apply(dto.getId()));
    }

    @Override
    public void deleteById(Long id) {
        securityPatrolSubjectDao.deleteById(id);

        CacheKit.invalidRedisCache(SECURITY_PATROL_SUBJECT_CACHE_BY_ID_FUNC.apply(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
